<?php
class controller_admin_resource_advertise extends system_user_controller_advertise {	

	public function action_index() {
		$this->_goRoute('resource/advertise/list');
		$this->render();
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$this->view->row = $row;
		$row->setWidth('120');
		$row->setHeight('120');
		$row = $this->_saveRow($row);
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/advertise/list'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Advertisement'))->setRoute('resource/advertise/addnew'));
		$this->render('resource/advertise/row.phtml');
	}
	
	public function action_edit() {
		$row = $this->_getRow();
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/advertise/list'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Edit Advertisement'))->setRoute('resource/advertise/edit')->setParams(array('id' => $row->getId())));
		$this->render('resource/advertise/row.phtml');
	}
	
	public function action_item() {
		$row = $this->_getRow();
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/advertise/list'));
		$this->_crumbs()->addPage(system_nav_page::create($row->getName())->setRoute('resource/advertise/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Item'))->setRoute('resource/advertise/item')->setParams(array('id' => $row->getId())));
			
		if($this->_param('o') == 'addnew') {
			$this->_itemAddnew($row);
		} elseif($this->_param('o') == 'edit') {
			$this->_itemEdit($row);
		} else {
			$this->_itemList($row);
		}
	}
	
	public function action_list() {
		$this->_batchDelete($this->concept, 'id', 'advertise.id');
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		$adapter->setLimit(30);
		if(1 && $_device = trim($this->_get('device'))) {
			try {
				$device = new system_setting_client($_device);
				$in = "SELECT `item_id` FROM `resource_advertise_device` WHERE `device_key` = '{$device->getKey()}'";
				$in = "SELECT `advertise_id` FROM `resource_advertise_item` WHERE `id` IN($in)";
				$adapter->where("advertise.id IN($in)");
			} catch (Exception $e) {
				
			}
		}
		$this->view->order = $order = strtolower($this->_get('order'));
		$_order = array("advertise.id DESC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			$_sort = !$sort ? 'DESC' : 'ASC';
			if($order == 'id') $order = 'advertise.id';
			if($order == 'click') $order = 'link.click';
			$_order = array("$order $_sort");
		}
		$adapter->order($_order);
		$this->view->list = new system_paginator($adapter);
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/advertise/list'));
		$this->render();
	}
	
}
